local pingjian = fk.CreateSkill{
  name = "mod_ty__pingjian",
}
---@param player ServerPlayer
local addTYPingjianSkill = function(player, skill_name)
  local room = player.room
  local skill = Fk.skills[skill_name]
  if skill == nil or player:hasSkill(skill_name, true) then return false end
  room:handleAddLoseSkills(player, skill_name, nil)
  local pingjian_skills = player:getTableMark("mod_ty__pingjian_skills")
  table.insertIfNeed(pingjian_skills, skill_name)
  room:setPlayerMark(player, "mod_ty__pingjian_skills", pingjian_skills)
  local pingjian_skill_times = player:getTableMark("mod_ty__pingjian_skill_times")
  table.insert(pingjian_skill_times, {skill_name, player:usedSkillTimes(skill_name)})
  for _, s in ipairs(skill.related_skills) do
    table.insert(pingjian_skill_times, {s.name, player:usedSkillTimes(s.name)})
  end
  room:setPlayerMark(player, "mod_ty__pingjian_skill_times", pingjian_skill_times)
end

---@param player ServerPlayer
local removeTYPingjianSkill = function(player, skill_name)
  local room = player.room
  local skill = Fk.skills[skill_name]
  if skill == nil then return false end
  room:handleAddLoseSkills(player, "-" .. skill_name, nil)
  local pingjian_skills = player:getTableMark("mod_ty__pingjian_skills")
  table.removeOne(pingjian_skills, skill_name)
  room:setPlayerMark(player, "mod_ty__pingjian_skills", pingjian_skills)
  local invoked = false
  local pingjian_skill_times = player:getTableMark("mod_ty__pingjian_skill_times")
  local record_copy = {}
  for _, pingjian_record in ipairs(pingjian_skill_times) do
    if #pingjian_record == 2 then
      local record_name = pingjian_record[1]
      if record_name == skill_name or not table.every(skill.related_skills, function (s)
          return s.name ~= record_name end) then
        if player:usedSkillTimes(record_name) > pingjian_record[2] then
          invoked = true
        end
      else
        table.insert(record_copy, pingjian_record)
      end
    end
  end
  room:setPlayerMark(player, "mod_ty__pingjian_skill_times", record_copy)
end
local function getPingjianSkills(player, event)
  local list = {
    ["play"] = {
      "qiangwu", "qiangxi", "luanji", "ty_ex__sanyao", "ol__xuehen", "ty__fenyue", "ex__yijue", "daoshu", "m_ex__xianzhen",
      "tianyi", "zhanjue", "mansi", "ty__lianji", "ty_ex__wurong", "lianhuan", "jiuchi", "ty_ex__zenhui", "duwu", "xuezhao", "kurou", "jianyan", "ty_ex__mieji",
      "ex__zhiheng", "ex__guose", "ty_ex__shenxing", "songci", "guolun", "duliang", "gongxin", "lueming", "jijie", "busuan", "minsi", "ty__lianzhu",
      "ex__fanjian", "tanbei", "ty__qingcheng", "jinhui", "chengshang", "weimeng", "ty__songshu", "poxi", "ty_ex__ganlu", "qixi", "ty__kuangfu", "qice",
      "ty_ex__gongqi", "ty_ex__huaiyi", "dingpan", "shanxi", "cuijian", "tiaoxin", "qingnang", "quji", "ty_ex__anguo", "limu", "ex__jieyin",
      "ty_ex__anxu", "ty_ex__mingce", "ziyuan", "lijian", "mingjian", "ex__rende", "mizhao", "yanjiao", "dimeng",
      "zhijian", "quhu", "tunan", "nuchen", "kanji", "duanliang", "huoji", "feijun", "ty_ex__pindi", "chaofeng", "yingshui", "qiongying", "zigu", "weiwu", "chiying",
    },
    [fk.Damaged] = {
      "ty_ex__yaoming", "danxin", "guixin", "ty__benyu", "fankui", "ex__ganglie", "ex__yiji", "ex__jianxiong", "ty_ex__enyuan", "jijing", "chouce", "jieming",
      "fangzhu", "jiushi", "ty_ex__chengxiang", "huituo", "ty__wangxi", "yuce", "zhichi", "ty_ex__zhiyu", "yachai", "wanggui", "qianlong", "peiqi",
    },
    [fk.EventPhaseStart] = {
      "junbing", "ty_ex__zhiyan", "tunjiang", "ex__biyue", "ty_ex__jingce", "zhenxing", "moshi", "zuilun", "fujian", "kunfen", "ol_ex__jushou", "zhengu", "os_ex__bingyi", "miji",
      "juece", "huimin","sp__youdi", "kuanshi", "ty__jieying", "suizheng", "ty_ex__zhenjun", "jujian", "daiyan", "nuanhui", "ty__qinqing"
    },
  }
  local used_skills = player:getTableMark("mod_ty__pingjian_used_skills")
  return table.filter(list[event] or {}, function (skill_name)
    return Fk.skills[skill_name] and not table.contains(used_skills, skill_name) and not player:hasSkill(skill_name, true)
  end)
end
local pingjian_ = {
  prompt = "#mod_ty__pingjian-active",
  card_num = 0,
  target_num = 0,
  can_use = function(self, player)
    return player:getMark("mod_ty__pingjian_used-phase") == 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    room:setPlayerMark(player, "mod_ty__pingjian_used-phase", 1)
    local skills = getPingjianSkills(player, "play")
    if #skills == 0 then return false end
    local choices = table.random(skills, 3)
    local skill_name = room:askForChoice(player, choices, self.name, "#mod_ty__pingjian-choice", true)
    local used_skills = player:getTableMark("mod_ty__pingjian_used_skills")
    table.insert(used_skills, skill_name)
    room:setPlayerMark(player, "mod_ty__pingjian_used_skills", used_skills)
    local phase_event = room.logic:getCurrentEvent():findParent(GameEvent.Phase)
    if phase_event ~= nil then
      addTYPingjianSkill(player, skill_name)
      phase_event:addCleaner(function()
        removeTYPingjianSkill(player, skill_name)
      end)
    end
  end,
}
local pingjian_trigger = {
  name = "#mod_ty__pingjian_trigger",
  events = {fk.Damaged, fk.EventPhaseStart},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill("mod_ty__pingjian") or player ~= target then return false end
    if event == fk.Damaged then
      return true
    elseif event == fk.EventPhaseStart then
      return player.phase == Player.Finish
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, "mod_ty__pingjian")
    player:broadcastSkillInvoke("mod_ty__pingjian")
    local skills = getPingjianSkills(player, event)
    if #skills == 0 then return false end
    local choices = table.random(skills, 3)
    local skill_name = room:askForChoice(player, choices, "mod_ty__pingjian", "#mod_ty__pingjian-choice", true)
    local used_skills = player:getTableMark("mod_ty__pingjian_used_skills")
    table.insert(used_skills, skill_name)
    room:setPlayerMark(player, "mod_ty__pingjian_used_skills", used_skills)
    local skill = Fk.skills[skill_name]
    if skill == nil then return false end

    addTYPingjianSkill(player, skill_name)
    if skill:triggerable(event, target, player, data) then
      skill:trigger(event, target, player, data)
    end
    removeTYPingjianSkill(player, skill_name)
  end,
}
local pingjian_invalidity = {
  invalidity_func = function(self, player, skill)
    local pingjian_skill_times = player:getTableMark("mod_ty__pingjian_skill_times")
    return table.find(pingjian_skill_times, function (pingjian_record)
      if #pingjian_record == 2 then
        local skill_name = pingjian_record[1]
        if skill.name == skill_name or not table.every(skill.related_skills, function (s)
          return s.name ~= skill_name end) then
            return player:usedSkillTimes(skill_name) > pingjian_record[2]
        end
      end
    end)
  end
}
pingjian:addEffect("active", pingjian_)
pingjian:addEffect(fk.Damaged, pingjian_trigger)
pingjian:addEffect(fk.EventPhaseStart, pingjian_trigger)
pingjian:addEffect("invalidity", pingjian_invalidity)
return pingjian